﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Reflection;
namespace SysCommon.Tools
{
    public class ArrayToDataSet
    {
        public static DataSet ObjectArrayToDataSet(object[] objArr)
        {
            if (objArr.Length == 0)
                return null;
            DataSet ds = CreateDataSet(objArr[0].GetType());
            ds = FillDataSet(ds, objArr);
            return ds;
        }

        public static DataSet CreateDataSet(Type t)
        {
            try
            {
                DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                ds.Tables.Add(dt);

                PropertyInfo[] pis = t.GetProperties();
                foreach (PropertyInfo pi in pis)
                {
                    DataColumn dc = new DataColumn(pi.Name, pi.PropertyType);
                    dt.Columns.Add(dc);
                }
                return ds;
            }
            catch (Exception ex)            
            {
                SysCommon.Global.mainLog.Error("调用CreateDataSet(Type t)失败", ex);
                return null;
            }
        }

        public static DataSet FillDataSet(DataSet ds, object[] objArr)
        {
            try
            {
                DataColumnCollection dcs = ds.Tables[0].Columns;

                Type t = objArr[0].GetType();
                foreach (object obj in objArr)
                {
                    DataRow dr = ds.Tables[0].NewRow();
                    for (int i = 0; i < dcs.Count; i++)
                    {
                        dr[i] = t.InvokeMember(dcs[i].ColumnName, BindingFlags.GetProperty, null, obj, null);
                    }
                    ds.Tables[0].Rows.Add(dr);
                }
                return ds;
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用FillDataSet(DataSet ds, object[] objArr)失败", ex);
                return ds;
            }
        }
        public static void FillDataSet(ref DataSet ds, object[] objArr,Type type)
        {
            try
            {
                try
                {
                    if (ds == null || ds.Tables[0].Rows.Count != objArr.Length)
                    {
                        ds = CreateDataSet(type);
                        ds = FillDataSet(ds, objArr);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    SysCommon.Global.mainLog.Error("调用出错", ex);
                }

                DataColumnCollection dcs = ds.Tables[0].Columns;
                Type t = objArr[0].GetType();
                for (int i = 0; i < objArr.Length; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    for (int j = 0; j < dcs.Count; j++)
                    {
                        dr[j] = t.InvokeMember(dcs[j].ColumnName, BindingFlags.GetProperty, null, objArr[i], null);
                    }
                }
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用FillDataSet(ref DataSet ds, object[] objArr)失败", ex);
            }
        }

        public static DataSet EntityToDataSet(Type t,object[] etys)
        {
            try
            {
                DataSet ds = CreateDataSet(t);
                ds = FillDataSet(ds, etys);

                return ds;
            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用EntityToDataSet(object[] etys)失败", ex);
                return null;
            }
        }
    }
}

